package com.qch.rabbitmq.demo;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeoutException;

/**
 * @Author qch
 * @Date 2021/5/14
 */
public class Producer {
    private static final String[] LOG_LEVEL={"info","error","warn"};
    private static final String[] LOG_AREA={"beijing","shanghai","shenzhen"};
    private static final String[] LOG_BIZ={"edu-online","biz-online","emp-online"};
    private static Random random = new Random();
    public static void main(String[] args) throws Exception {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("ex.topic",BuiltinExchangeType.TOPIC,true,false,null);

        String area,level,biz;
        String routingkey,message;
        for (int i = 0; i <100 ; i++) {
            area = LOG_AREA[random.nextInt(LOG_AREA.length)];
            level = LOG_LEVEL[random.nextInt(LOG_LEVEL.length)];
            biz = LOG_BIZ[random.nextInt(LOG_BIZ.length)];
            routingkey=area+"."+biz+"."+level;
            message="LOG:["+level+"]:这是["+area+"]地区["+biz+"]服务器发来的消息。MSG_SEQ="+i;
            channel.basicPublish("ex.topic",//exchange
                    routingkey,//routingKey
                     null,
                      message.getBytes("utf-8"));
        }
        channel.close();
        connection.close();
    }
}
